package com.csvc.business.cmdb.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.csvc.business.cmdb.Constants;
import com.csvc.commons.dto.MessageDto;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.http.HttpStatus;

@Service
public class EmailService {

	private Logger logger = LoggerFactory.getLogger(EmailService.class);

	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${noti.url}")
	private String url;

	ObjectMapper mapper = new ObjectMapper();

	public boolean invoideNotifyRestAPI(MessageDto dto) {
		logger.info("url:{}alarm dto-------------->",url,dto);
		HttpHeaders headers = new HttpHeaders();
		// 这里设置的是以payLoad方式提交数据，对于Payload方式，提交的内容一定要是String，且Header要设为“application/json”
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		try {
			String value = mapper.writeValueAsString(dto);
			HttpEntity<String> requestEntity = new HttpEntity<String>(value, headers);

			ResponseEntity<String> res = restTemplate.postForEntity(url + Constants.NOTIFICATION_API, requestEntity,
					String.class);
			logger.info("response code:{}",res.getStatusCode());
			if (res.getStatusCode().value() == HttpStatus.HTTP_CREATED)
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			//throw new RuntimeException(e.getMessage());
		}
	}

	
}